<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  activity_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-17.
//  Copyright 2011 lishuzu. All rights reserved.
// 
class Ship_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->userinfo = $this->session->userdata('user');
		}
		
		
		
		//得到当前uid所有关注的人的UID
		function get_followuids($uid, $limit = ''){
			$data = $ship =  array();
			if ($uid) {
				$this->db->select('followid');
				$this->db->where('uid', $uid);
				if($limit){
					$this->db->limit($limit);
				}
				$query = $this->db->get('ship');
				foreach ($query->result_array() as $row) {
					$ship[] = $row['followid'];
				}
				if ($ship) {
					return $ship;
				}else{
					return false;
				}
			}
			return false;
		}
		
		
		//得到当前uid所有关注的人
		function get_follows($uid, $num = '20', $offset = ''){
			$data = $ship =  array();
			if ($uid) {
				$offset = $offset ? $offset : 0;
				$limit = " limit ".$offset.", $num";
				$this->db->where('uid', $uid);
				$data['count'] = $this->db->count_all_results('ship');
				$this->db->where('uid', $uid);
				$sql = "SELECT s.id, u.uid, u.name, u.follow_count, u.fans_count, u.likenum, u.worknum FROM ".$this->db->dbprefix('ship')." s LEFT JOIN ".$this->db->dbprefix('user')." u ON s.followid=u.uid Where s.uid='".$uid."' ORDER BY s.dateline DESC $limit";
				$query = $this->db->query($sql);
				foreach ($query->result_array() as $row) {
					$row['avatar_small'] = avatar($row['uid'], 'small');
					$ship[$row['uid']] = $row;
				}
				
				$data['ship'] = $ship;
				
				if ($data) {
					return $data;
				}else{
					return false;
				}
			}
			return false;
		}
		
		
		//得到当前uid的粉丝
		function get_fans($uid, $num = '20', $offset = ''){
			$data = $ship =  array();
			if ($uid) {
				$offset = $offset ? $offset : 0;
				$limit = " limit ".$offset.", $num";
				$this->db->where('followid', $uid);
				$data['count'] = $this->db->count_all_results('ship');
				$this->db->where('uid', $uid);
				$sql = "SELECT s.id, u.uid, u.name, u.follow_count, u.fans_count, u.likenum, u.worknum FROM ".$this->db->dbprefix('ship')." s LEFT JOIN ".$this->db->dbprefix('user')." u ON s.uid=u.uid Where s.followid='".$uid."' ORDER BY s.dateline DESC $limit";
				$query = $this->db->query($sql);
				foreach ($query->result_array() as $row) {
					$row['avatar_small'] = avatar($row['uid'], 'small');
					$ship[$row['uid']] = $row;
				}
				
				$data['ship'] = $ship;
				if ($data) {
					return $data;
				}else{
					return false;
				}
			}
			return false;
		}
		
		//得到关注关系（当前人是否关注了查询人）
		function get_follow($uid){
			$ship = array();
			$this->db->where('followid', $uid);
			$this->db->where('uid', $this->userinfo['uid']);
			$query = $this->db->get('ship');
			$ship = $query->row_array();
			if ($ship) {
				return $ship;
			}else{
				return false;
			}
		}
		
		//得到粉丝关系 (当前人是否是查询uid的粉丝)
		function get_fan($uid){
			$ship = array();
			$this->db->where('uid', $uid);
			$this->db->where('followid', $this->userinfo['uid']);
			$query = $this->db->get('ship');
			$ship = $query->row_array();
			if ($ship) {
				return $ship;
			}else{
				return false;
			}
		}
		
		function get_ship($uid){
			if ($uid) {
				if ($this->userinfo) {
					if ($this->get_follow($uid) && !$this->get_fan($uid)) {
						//已经关注
						return 1;
					}elseif(!$this->get_follow($uid) && $this->get_fan($uid)){
						//我是粉丝 被关注
						return 2;
					}elseif ($this->get_follow($uid) && $this->get_fan($uid)) {
						// 互相关注
						return 3;
					}elseif(!$this->get_follow($uid) && !$this->get_fan($uid)) {
						//互相都没有关注
						return 4;
					}
				}else{
					// 未登录
					return 5;
				}
			}
			return false;
		}
		
		function get_ship_word($id){
			$ship = array(
						'1' => '已关注,取消',
			 			'2' =>'加关注',
			 			'3' =>'互相关注，取消',
			 			'4' =>'加关注',
			 			'5' =>'登录加关注',
			);
			if (isset($ship[$id])) {
				return $ship[$id];
			}
			return false;
		}
		
		function add($ship){
			if ($ship) {
				$this->db->insert('ship', $ship);
				return $this->db->insert_id();
			}else{
				return false;
			}
		}
		
		function delete($id){
			if ($id) {
				$this->db->where('id', $id);
				$this->db->delete('ship');
				return $this->db->affected_rows();
			}else{
				return false;
			}
		}
}
?>